﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using Microsoft.TeamFoundation.TestManagement.Client;

namespace TFSTools.TFSLibrary.TestManagement
{
    public class TFSTestIterationRow
    {
        internal TFSTestIterationRow(int index, int count, TFSTestIterationDataItem[] replacementValues)
        {
            if (index >= 0)
            {
                this.Index = index;
                this.Name = "Iteration " + (index + 1).ToString() + " of " + count;
                this.ReplacementValues = replacementValues;
            }
            else
            {
                this.Index = 0;
                this.Name = "Iteration 1 of 1";
                this.ReplacementValues = new TFSTestIterationDataItem[0];
            }
        }

        public int Index { get; private set; }

        public string Name { get; private set; }

        public TFSTestIterationDataItem[] ReplacementValues { get; private set; }

        public override string ToString()
        {
            return this.Name;
        }
    }

    public class TFSTestIterationDataItem
    {
        internal TFSTestIterationDataItem(string paramName, string replacementValue)
        {
            this.ParameterName = paramName;
            this.ReplacementValue = replacementValue;
        }

        public string ParameterName { get; private set; }

        public string ReplacementValue { get; private set; }
    }

    public class TFSTestIterationRowCollection : ReadOnlyCollection<TFSTestIterationRow>
    {
        public int ParameterCount { get; private set; }

        internal TFSTestIterationRowCollection(IList<TFSTestIterationRow> list, int parameterCount)
            : base(list)
        {
            this.ParameterCount = parameterCount;
        }

        internal static TFSTestIterationRowCollection Create(ITestCase tfsTestCase)
        {
            var result = new List<TFSTestIterationRow>();
            int parameterCount = 0;

            if (tfsTestCase.Data != null &&
                tfsTestCase.Data.Tables != null &&
                tfsTestCase.Data.Tables.Count > 0 &&
                tfsTestCase.Data.Tables[0].Rows != null)
            {
                var data = tfsTestCase.Data.Tables[0];

                var dataColumns = from testParam
                                  in tfsTestCase.TestParameters
                                  select data.Columns[testParam.Name];

                parameterCount = dataColumns.Count();

                var rows = data.Rows;
                var count = rows.Count;

                for (int index = 0; index < count; index++)
                {
                    var dataRow = rows[index];
                    var dataItems = new List<TFSTestIterationDataItem>(parameterCount);

                    foreach (var column in dataColumns)
                    {
                        dataItems.Add(new TFSTestIterationDataItem(column.ColumnName, dataRow[column].ToString()));
                    }

                    result.Add(new TFSTestIterationRow(index, count, dataItems.ToArray()));
                }
            }

            if (result.Count == 0)
            {
                result.Add(new TFSTestIterationRow(-1, 0, null));
            }

            return new TFSTestIterationRowCollection(result, parameterCount);
        }
    }
}
